package a66weding.com.jiehuntong.dao.db.address;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import a66weding.com.jiehuntong.beans.ProvinceBean;
import a66weding.com.jiehuntong.beans.ProvinceCitySelectBean;
import a66weding.com.jiehuntong.common.utils.FileUtil;
import a66weding.com.jiehuntong.dao.db.TastOpenHelper;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/8/9
 * @description :
 */
public class AddressDao  {

    private Context context;
    private File savePath;

    private  AddressOpenHelper helper;

    public AddressDao(Context context){
        this.context=context;
        helper = new AddressOpenHelper(context);

    }


    public boolean addAllAddress(List<ProvinceCitySelectBean.ResultBean> provinceList){

        if(provinceList==null) {
            return false;
        }
        SQLiteDatabase db = helper.getWritableDatabase();
        try{

        String sql = "insert into address(i_ID,provinceName,provinceID,is_PianYuan,b_OpenBusinessArea,cityID,cityi_ID," +
                "cityName,nvc_FirstLetter,areaID,areai_ID,areaName) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        SQLiteStatement stat = db.compileStatement(sql);
        db.beginTransaction();

        int isPianyuan=0;
        int isOpenBusinessArea=0;


        for (int i = 0; i < provinceList.size(); i++) {

            List<ProvinceCitySelectBean.ResultBean.CitysBean> cityList = provinceList.get(i).getCitys();

            for (int j = 0; j < cityList.size(); j++) {

                List<ProvinceCitySelectBean.ResultBean.CitysBean.AreasBean> areaList = cityList.get(j).getAreas();

                for (int k = 0; k < areaList.size(); k++) {

                    stat.bindLong(1, provinceList.get(i).getI_ID());
                    stat.bindString(2,provinceList.get(i).getProvinceName());
                    stat.bindLong(3, provinceList.get(i).getProvinceID());

                    boolean is_pianYuan = provinceList.get(i).isIs_PianYuan();
                    if(is_pianYuan) {
                        isPianyuan=1;
                    }else{
                        isPianyuan=0;
                    }
                    stat.bindLong(4, isPianyuan);
                    boolean b_OpenBusinessArea = provinceList.get(i).isB_OpenBusinessArea();
                    if(b_OpenBusinessArea) {
                        isOpenBusinessArea=1;
                    }else{
                        isOpenBusinessArea=0;
                    }
                    stat.bindLong(5, isOpenBusinessArea);


                    stat.bindLong(6, cityList.get(j).getCityID());
                    stat.bindLong(7, cityList.get(j).getI_ID());
                    stat.bindString(8, cityList.get(j).getCityName());
                    stat.bindString(9, cityList.get(j).getNvc_FirstLetter());

                    stat.bindLong(10, areaList.get(k).getAreaID());
                    stat.bindLong(11, areaList.get(k).getI_ID());
                    stat.bindString(12, areaList.get(k).getAreaName());

                    stat.executeInsert();
                }

            }


        }

            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(db!=null) {
                db.close();
            }
        }



    }







    public  void addProvince( ProvinceBean.DataBean dataBean) {
        SQLiteDatabase db = helper.getWritableDatabase();
        dataBean.getI_ID();
        int isPianyuan=0;
        boolean is_pianYuan = dataBean.isIs_PianYuan();
        if(is_pianYuan) {
            isPianyuan=1;
        }else{
            isPianyuan=0;
        }

        try{
            String sql="insert into address(i_ID,provinceName,provinceID,is_PianYuan) values(?,?,?,?) ";
            db.execSQL(sql,new Object[]{dataBean.getI_ID(),dataBean.getProvinceName(),dataBean.getProvinceID(),isPianyuan} );
            db.close();

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(db!=null) {
                db.close();
            }
        }

    }


    public  boolean addListProvince(List<ProvinceBean.DataBean> provinceList) {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(provinceList==null) {
            return false;
        }
        try{
            String sql = "insert into address(i_ID,provinceName,provinceID,is_PianYuan) values(?,?,?,?)";
            SQLiteStatement stat = db.compileStatement(sql);
            db.beginTransaction();
            int isPianyuan=0;
            for (ProvinceBean.DataBean dataBean : provinceList) {
                stat.bindLong(1, dataBean.getI_ID());
                stat.bindString(2,dataBean.getProvinceName());
                stat.bindLong(3, dataBean.getProvinceID());
                boolean is_pianYuan = dataBean.isIs_PianYuan();
                if(is_pianYuan) {
                    isPianyuan=1;
                }else{
                    isPianyuan=0;
                }
                stat.bindLong(4, isPianyuan);
                stat.executeInsert();
            }
            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(db!=null) {
                db.close();
            }
        }

    }


    public boolean isPianyuanAddress( int i_Id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        boolean isPianyuan=false;
        try{
            String sql="select is_PianYuan from address where i_ID=?";
            Cursor cursor = db.rawQuery(sql,new String[]{i_Id+""} );

            if(cursor!=null && cursor.getCount()>0) {
                while(cursor.moveToNext()){
                    int is_pianYuan = cursor.getInt(cursor.getColumnIndex("is_PianYuan"));
                    if(is_pianYuan==0) {
                        isPianyuan=false;
                    }else{
                        isPianyuan=true;
                    }
                }
            }
            cursor.close();
            db.close();

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(db!=null) {
                db.close();
            }
        }
        return isPianyuan;

    }



    public  Set<String> getProvinceList() {

        Set<String> provinceList = new HashSet<String>();

        SQLiteDatabase db = helper.getWritableDatabase();

        try {
            Cursor cursor = db.query("address", new String[]{"provinceName"}, null, null, null, null, null);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) {
                    String provinceName = cursor.getString(cursor.getColumnIndex("provinceName"));
                    provinceList.add(provinceName);
                }
            }
            return provinceList;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(db!=null) {
                db.close();
            }
        }

        return null;
    }

    public  Set<String> getCityListByPName(String pName) {

        Set<String> cityList = new HashSet<String>();

        SQLiteDatabase db = helper.getWritableDatabase();

        try {
            Cursor cursor = db.query("address", new String[]{"cityName"}, "provinceName=?", new String[]{pName}, null, null, null);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) {
                    String cityName = cursor.getString(cursor.getColumnIndex("cityName"));
                    cityList.add(cityName);
                }
            }
            return cityList;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(db!=null) {
                db.close();
            }
        }

        return null;
    }


    public Set<String> getAreaListByPNameCName(String pName, String cName) {

        Set<String> areaList = new HashSet<String>();

        SQLiteDatabase db = helper.getWritableDatabase();

        try {
            Cursor cursor = db.query("address", new String[]{"areaName"}, "provinceName=? and cityName=?", new String[]{pName,cName}, null, null, null);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) {
                    String areaName = cursor.getString(cursor.getColumnIndex("areaName"));
                    areaList.add(areaName);
                }
            }
            return areaList;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(db!=null) {
                db.close();
            }
        }

        return null;

    }
}
